// pages/my_discount/my_discount.js
var common = require("../../common/common.js").common;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        label: 0,
        discount: [],
        discount2: [],
        discount3: [],
        rp_name: "",
        vouchers: [],
        page: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        wx.hideShareMenu();
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        this.getData();
        this.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getData();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    changeLabel: function(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var openid = wx.getStorageSync("user").openid;
        that.setData({
            label: id,
            page: 1,
            vouchers: []
        });
        that.getData();
    },
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Voucher/voucher/myvoucher",
            method: "post",
            data: {
                openid: openid,
                status: that.data.label,
                page: that.data.page
            },
            success: res => {
                let arr = that.data.vouchers;
                console.log(arr);
                let vouchers = res.extra.vouchers;
                arr = that.data.page == 1 ? vouchers : arr.concat(vouchers);
                console.log(arr);
                that.setData({
                    discount: res.extra,
                    vouchers: arr,
                    page: that.data.page + 1
                });
                console.log(that.data.vouchers);
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                wx.setNavigationBarTitle({
                    title: "我的" + res.extra.rp_name
                });
                wx.setNavigationBarColor({
                    frontColor: "#ffffff",
                    backgroundColor: res.extra.color_main
                });
                that.setData({
                    rp_name: res.extra.rp_name
                });
            }
        });
    }
});